package cn.picclife.mwx.common.core.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import cn.picclife.mwx.common.core.util.DateTimeUtils;

/**
 * 日期处理工具类
 * 
 * @ClassName: DateUtils 
 * @Description:  
 * @author Mengmeng.Kang
 * @date 2016年9月20日 上午9:41:56 
 *
 */
public class DateUtils {
    /**
     * 获取年份
     * 
     * @Title: getYear 
     * @Description: 
     * @param date 日期
     * @return String 
     */
    public static String getYear(Date date) {
        if (date == null) {
            return "";
        } else {
            return DateTimeUtils.dateToString(date, "yyyy");
        }

    }

    /**
     * 获取月份
     * 
     * @Title: getMonth 
     * @Description: 
     * @param date
     * @return String 
     */
    public static String getMonth(Date date) {
        if (date == null) {
            return "";
        } else {
            return DateTimeUtils.dateToString(date, "MM");
        }
    }

    /**
     * 获取日期
     * 
     * @Title: getDay 
     * @Description: 
     * @param date
     * @return String 
     */
    public static String getDay(Date date) {
        if (date == null) {
            return "";
        } else {
            return DateTimeUtils.dateToString(date, "dd");
        }

    }

    /**
     * 将String转换为Date类型（默认日期格式为yyyy-MM-dd）
     * 
     * @Title: parseDate 
     * @Description: 
     * @param date
     * @return Date 
     */
    public static Date parseDate(String date) {
        return parseDate(date, null);
    }

    /**
     * 将String转换为Date类型
     * 
     * @Title: parseDate 
     * @Description: 
     * @param date 
     * @param format 日期格式
     * @return Date 
     */
    public static Date parseDate(String date, String format) {
        if (date == null || date.equals("")) {
            return null;
        }
        try {
            if (format == null || format.equals("")) {
                return DateTimeUtils.parseDateStrictly(date, "yyyy-MM-dd");
            } else {
                return DateTimeUtils.parseDateStrictly(date, format);
            }
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * Date转String类型（默认日期格式yyyy-MM-dd）
     * 
     * @Title: formatDate 
     * @Description: 
     * @param date
     * @return String 
     */
    public static String formatDate(Date date) {
        return formatDate(date, null);
    }


    /**
     * Date转String日期类型
     * 
     * @Title: formatDate 
     * @Description: 
     * @param date
     * @return String 日期格式
     */
    public static String formatDate(String date) {
        return formatDate(parseDate(date), null);
    }

    /**
     * Date转String时间类型（默认时间格式yyyy-MM-dd HH:mm:ss）
     * 
     * @Title: formatTime 
     * @Description: 
     * @param date
     * @return String 
     */
    public static String formatTime(Date date) {
        return formatTime(date, null);
    }

    /**
     * Date类型转String时间类型
     * 
     * @Title: formatTime 
     * @Description: 
     * @param date
     * @param format 时间格式
     * @return String 
     */
    public static String formatTime(Date date, String format) {
        if (date == null) {
            return "";
        } else {
            if (format == null || format.equals("")) {
                return DateTimeUtils.dateToString(date, "yyyy-MM-dd HH:mm:ss");

            } else {
                return DateTimeUtils.dateToString(date, format);
            }
        }
    }

    /**
     * Date类型转换为String
     * 
     * @Title: formatDate 
     * @Description: 
     * @param date
     * @param format 日期格式
     * @return String 
     */
    public static String formatDate(Date date, String format) {
        if (date == null) {
            return "";
        } else {
            if (format == null || format.equals("")) {
                return DateTimeUtils.dateToString(date, "yyyy-MM-dd");
            } else {
                return DateTimeUtils.dateToString(date, format);
            }
        }
    }

    /**
     * 转类型java.util.Date为java.sql.Date
     * 
     * @param date
     * @return
     */
    public static java.sql.Date getSqlDate(Date date) {
        if (date == null) {
            return null;
        }
        return new java.sql.Date(date.getTime());
    }

    /**
     * 转类型java.sql.Date为java.util.Date
     * 
     * @param date
     * @return
     */
    public static Date getUtilDate(java.sql.Date date) {
        if (date == null) {
            return null;
        }
        return new Date(date.getTime());
    }

    /**
     * 日期增加天
     * 
     * @Title: addDay 
     * @Description: 
     * @param oldDate 日期
     * @param addDays 增加天数
     * @return Date 
     */
    public static Date addDay(Date oldDate, int addDays) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(oldDate);
        calendar.add(Calendar.DATE, addDays);
        return calendar.getTime();
    }

    /**
     * 日期增加月
     * 
     * @Title: addDay 
     * @Description: 
     * @param oldDate 日期
     * @param addDays 增加天数
     * @return Date 
     */
    public static String addMonth(int addMonths, Date oldDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(oldDate);
        calendar.add(Calendar.MONTH, addMonths);
        return formatDate(calendar.getTime());
    }

    /**
     * 日期增加天
     * 
     * @Title: addDay 
     * @Description: 
     * @param oldDate 日期
     * @param addDays 增加天数
     * @return String 
     */
    public static String addDay(String oldDate, int addDays) {
        Date date = parseDate(oldDate);
        date = addDay(date, addDays);
        return formatDate(date);

    }

    /**
     * 根据生日获取年龄（周岁）
     * 
     * @Title: getAge 
     * @Description: 
     * @param birthday 生日Date
     * @param endDate 当前日期Date
     * @return int 
     */
    public static int getAge(Date birthday, Date endDate) {
        return calInterval(birthday, endDate, "Y");
    }

    /**
     * 根据生日获取年龄（周岁）
     * 
     * @Title: getAge 
     * @Description: 
     * @param birthday 生日String
     * @param endDate 当前日期String
     * @return int 
     */
    public static int getAge(String birthday, String endDate) {

        if (null != birthday && !"".equals(birthday)) {

            Date birDate = DateUtils.parseDate(birthday);
            return DateUtils.getAge(birDate, DateUtils.parseDate(endDate));
        }
        return 0;
    }

    /**
     * 根据生日获取年龄（周岁）
     * 
     * @Title: getAge 
     * @Description: 
     * @param birthday 生日String
     * @param endDate 当前日期Date
     * @return int 
     */
    public static int getAge(String birthday, Date endDate) {

        if (null != birthday && !"".equals(birthday)) {

            Date birDate = DateUtils.parseDate(birthday);
            return DateUtils.getAge(birDate, endDate);
        }
        return 0;
    }

    /**
     * 根据生日获取年龄（天数）
     * 
     * @Title: getAgeByDay 
     * @Description: 
     * @param birthday
     * @param endDate
     * @return int 
     */
    public static int getAgeByDay(Date birthday, Date endDate) {
        return calInterval(birthday, endDate, "D");
    }

    /**
     * 判断是否闰年
     * 
     * @Title: isLeapYear 
     * @Description: 
     * @param nYear
     * @return boolean
     */
    public static boolean isLeapYear(int nYear) {
        return (nYear % 400 == 0) || ((nYear % 100 != 0) && (nYear % 4 == 0));
    }

    /**
     * 根据开始日期和结束日期获取不同单位粒度下的两个日期只差
     * 
     * @Title: calInterval 
     * @Description: 
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param unit 单位("Y"--年  "M"--月   "D"--日  "H"--时  "m"--分  "S"--秒)
     * @return int
     */
    public static int calInterval(Date startDate, Date endDate, String unit) {
        if (startDate == null || endDate == null) {
            return 0;
        }
        int interval = 0;
        GregorianCalendar sCalendar = new GregorianCalendar();
        sCalendar.setTime(startDate);
        int sYears = sCalendar.get(Calendar.YEAR);
        int sMonths = sCalendar.get(Calendar.MONTH);
        int sDays = sCalendar.get(Calendar.DAY_OF_MONTH);
        GregorianCalendar eCalendar = new GregorianCalendar();
        eCalendar.setTime(endDate);
        int eYears = eCalendar.get(Calendar.YEAR);
        int eMonths = eCalendar.get(Calendar.MONTH);
        int eDays = eCalendar.get(Calendar.DAY_OF_MONTH);
        if (unit != null && unit.trim().equals("Y")) {
            interval = eYears - sYears;
            if (eMonths < sMonths) {
                interval--;
            } else {
                if (eMonths == sMonths && eDays < sDays) {
                    interval--;
                    if (eMonths == 1) {
                        if (isLeapYear(sYears) && isLeapYear(eYears)) {
                            if (eDays == 28) {
                                interval++;
                            }
                        }
                    }
                }
            }
        }
        if (unit != null && unit.trim().equals("M")) {
            interval = eYears - sYears;
            interval *= 12;
            interval += eMonths - sMonths;

            if (eDays < sDays) {
                interval--;
                int maxDate = eCalendar.getActualMaximum(Calendar.DATE);
                if (eDays == maxDate) {
                    interval++;
                }
            }
        }
        if (unit != null && unit.trim().equals("D")) {
            sCalendar.set(sYears, sMonths, sDays);
            eCalendar.set(eYears, eMonths, eDays);
            long lInterval =
                    (eCalendar.getTime().getTime() - sCalendar.getTime().getTime()) / 86400000;
            interval = (int) lInterval;
        }
        if (unit != null && unit.trim().equals("H")) {
            long lInterval = (endDate.getTime() - startDate.getTime()) / 1000 / 60 / 60;
            interval = (int) lInterval;
        }
        if (unit != null && unit.trim().equals("m")) {
            long lInterval = (endDate.getTime() - startDate.getTime()) / 1000 / 60;
            interval = (int) lInterval;
        }
        if (unit != null && unit.trim().equals("S")) {

            long lInterval = (endDate.getTime() - startDate.getTime()) / 1000;
            interval = (int) lInterval;
        }
        return interval;
    }

    /**
     * 通过年龄获取生日
     * 
     * @Title: getBirthdayByAge 
     * @Description: 
     * @param age
     * @return String
     */
    public static String getBirthdayByAge(String age) {

        if (null != age && !"".equals(age)) {

            int currentAge = Integer.valueOf(age);
            Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int birYear = year - currentAge;
            c.set(Calendar.YEAR, birYear);

            if (c.get(Calendar.MONTH) == Calendar.FEBRUARY && c.get(Calendar.DAY_OF_MONTH) == 29) {

                int day = c.getMaximum(Calendar.DAY_OF_MONTH);
                c.set(Calendar.DAY_OF_MONTH, day);
            }

            Date currentDate = c.getTime();
            return DateUtils.formatDate(currentDate, null);
        }

        return null;
    }

    /**
     * 获取当前日期(java.util.Date)
     * 
     * @return
     */
    public static Date getCurrentDate() {
        return new Date(System.currentTimeMillis());
    }

    /**
     * 获取当前日期(java.sql.Date)
     * 
     * @return
     */
    public static java.sql.Date getCurrentSqlDate() {
        return new java.sql.Date(System.currentTimeMillis());
    }

    /**
     * 根据年月日时分秒信息转换为Date类型
     * 
     * @param year
     * @param month
     * @param date
     * @param hour
     * @param minuter
     * @param second
     * @return
     */
    public static Date toDate(int year, int month, int date, int hour, int minuter, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, date, hour, minuter, second);
        return calendar.getTime();
    }


    /**
     * 根据年月日信息转换为Date类型
     * 
     * @param year
     * @param month
     * @param date
     * @return
     */
    public static Date toDate(int year, int month, int date) {
        return toDate(year, month, date, 0, 0, 0);
    }

    /**
     * 将小于10的数值前添加"0"
     * 
     * @param num
     * @return
     */
    public static String fillWithZero(int num) {
        if (num >= 1 && num <= 9) {
            return "0" + num;
        }

        return num + "";
    }

    /**
     * 取当前周的第一天
     * 
     * @return
     */
    public static String getFirstDayOfWeek() {

        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
        Date date = new Date(c.getTimeInMillis());
        return formatDate(date);
    }

    /**
     * 获取当前周的最后一天
     * 
     * @return
     */
    public static String getLastDayOfWeek() {

        Calendar c = Calendar.getInstance();
        int lastDayofweek = c.getFirstDayOfWeek() + 6;
        c.set(Calendar.DAY_OF_WEEK, lastDayofweek);
        Date date = new Date(c.getTimeInMillis());
        return formatDate(date);
    }

    /**
     * 获取当前月的最后一天
     * 
     * @param date
     * @return
     */
    public static int getMaxDayOfMonth(java.util.Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * Description: 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
     * 
     * @param oldDate
     *            java.util.Date 锟斤拷锟斤拷
     * @param addYears
     *            int 锟斤拷锟接碉拷锟斤拷锟斤拷
     * @return java.util.Date 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
     */
    public static java.util.Date addYear(java.util.Date oldDate, int addYears) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(oldDate);
        calendar.add(Calendar.YEAR, addYears);
        return calendar.getTime();
    }

    /**
     * Description: 锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
     * 
     * @param oldDate
     *            Timestamp 时锟斤拷
     * @param addYears
     *            int 锟斤拷锟接碉拷锟斤拷锟斤拷
     * @return Timestamp 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞憋拷锟�
     */
    public static Timestamp addYear(Timestamp oldDate, int addYears) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new java.util.Date(oldDate.getTime()));
        calendar.add(Calendar.YEAR, addYears);
        return new Timestamp(calendar.getTime().getTime());
    }

    /**
     * Description: 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
     * 
     * @param oldDate
     *            java.util.Date 锟斤拷锟斤拷
     * @param addMonths
     *            int 锟斤拷锟接碉拷锟斤拷锟斤拷
     * @return java.util.Date 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
     */
    public static java.util.Date addMonth(java.util.Date oldDate, int addMonths) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(oldDate);
        calendar.add(Calendar.MONTH, addMonths);
        return calendar.getTime();
    }

    /**
     * Description: 锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
     * 
     * @param oldDate
     *            Timestamp 时锟斤拷
     * @param addMonths
     *            int 锟斤拷锟接碉拷锟斤拷锟斤拷
     * @return Timestamp 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞憋拷锟�
     */
    public static Timestamp addMonth(Timestamp oldDate, int addMonths) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new java.util.Date(oldDate.getTime()));
        calendar.add(Calendar.MONTH, addMonths);
        return new Timestamp(calendar.getTime().getTime());
    }

    /**
     * Description: 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
     * 
     * @param oldDate
     *            java.util.Date 锟斤拷锟斤拷
     * @param addWeeks
     *            int 锟斤拷锟接碉拷锟斤拷锟斤拷
     * @return java.util.Date 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
     */
    public static java.util.Date addWeek(java.util.Date oldDate, int addWeeks) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(oldDate);
        calendar.add(Calendar.WEEK_OF_YEAR, addWeeks);
        return calendar.getTime();
    }

    /**
     * Description: 锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
     * 
     * @param oldDate
     *            Timestamp 时锟斤拷
     * @param addWeeks
     *            int 锟斤拷锟接碉拷锟斤拷锟斤拷
     * @return Timestamp 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞憋拷锟�
     */
    public static Timestamp addWeek(Timestamp oldDate, int addWeeks) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new java.util.Date(oldDate.getTime()));
        calendar.add(Calendar.WEEK_OF_YEAR, addWeeks);
        return new Timestamp(calendar.getTime().getTime());
    }

    /**
     * wll 锟饺斤拷锟斤拷锟斤拷锟斤拷锟节的达拷小锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷诘诙锟斤拷锟斤拷锟斤拷锟絫rue锟斤拷锟斤拷锟津返伙拷false
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static boolean compareDate(Date date1, Date date2) {

        Long time1 = date1.getTime();
        Long time2 = date2.getTime();

        if (time1 > time2) {

            return true;
        } else {

            return false;
        }

    }

    /**
     * Description: 锟斤拷锟斤拷锟斤拷转锟斤拷锟斤拷锟斤拷"**锟斤拷**锟斤拷**锟斤拷"锟斤拷式
     * 
     * 说锟斤拷锟斤拷锟斤拷锟斤拷潞锟斤拷斩锟斤拷锟斤拷锟斤拷锟�9锟斤拷锟斤拷锟斤拷为****锟斤拷*锟斤拷*锟秸ｏ拷锟斤拷锟斤拷****锟斤拷0*锟斤拷0*锟斤拷
     * 
     * @param date
     *            java.util.Date 锟斤拷转锟斤拷锟斤拷锟斤拷锟斤拷
     * @return String 转锟斤拷锟斤拷锟斤拷锟斤拷锟�
     */
    public static String getChineseDate(java.util.Date date) {
        String result = "";

        if (date == null) {
            result = " 年 月 日";
        } else {
            result = "" + getYear(date) + "年" + getMonth(date) + "月" + getDay(date) + "日";
        }

        return result;
    }

}
